﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NCP.Recruiting.ApplicationServices.Implementations;
using NCP.Recruiting.WebApp.Models.Departments;
using NCP.Recruiting.ApplicationServices.Messaging.Settings;
using NCP.Recruiting.ApplicationServices.ViewModels;

namespace NCP.Recruiting.WebApp.Controllers {
    public class DepartmentController : AuthorizedController {
        private readonly CompanyAppService _companyAppService;
        private readonly SettingAppService _settingAppService;

        public DepartmentController(CompanyAppService companyAppService,
            SettingAppService settingAppService)
            : base(companyAppService) {
            this._companyAppService = companyAppService;
            this._settingAppService = settingAppService;
        }


        public ActionResult List(DepartmentPagingFilteringModel command) {
            var model = new DepartmentListModel();

            if (command.PageSize <= 0)
                command.PageSize = 10;
            if (command.PageNumber <= 0)
                command.PageNumber = 1;

            var request = new GetAllDepartmentsRequest(CurrentUserId);
            request.PageIndex = command.PageNumber - 1;
            request.PageCount = command.PageSize;

            var response = _settingAppService.GetAllDepartments(request);
            model.PagingFilteringContext.LoadPagedList(response.PagedDepartments);
            model.Departments = response.PagedDepartments;

            return View(model);
        }

        public ActionResult Add() {
            return View(new DepartmentFormModel());
        }

        [HttpPost]
        public ActionResult Add(DepartmentFormModel formModel) {
            if (ModelState.IsValid) {
                var request = new CreateDepartmentRequest();
                request.CompanyId = this.CurrentUserId;
                request.DepartmentProperties = formModel;

                var response = _settingAppService.CreateDepartment(request);

                return RedirectToAction("List");
            }

            return View();
        }

        public ActionResult Edit(Guid id) {
            var response = _settingAppService.GetDepartment(new GetDepartmentRequest(id));

            return View(ConstructForm(response.DepartmentProperties));
        }

        [HttpPost]
        public ActionResult Edit(DepartmentFormModel formModel) {
            if (TryUpdateDepartment(formModel)) {
                return RedirectToAction("List");
            }

            var fm = ConstructForm(_settingAppService.GetDepartment(new GetDepartmentRequest(formModel.Id)).DepartmentProperties);
            return View(fm);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id) {
            var request = new DeleteDepartmentRequest(id);
            var response = _settingAppService.DeleteDepartment(request);

            if (response.Exception == null) {
                RedirectToAction("List");
            }

            return RedirectToAction("List");
        }

        private bool TryUpdateDepartment(DepartmentFormModel formModel) {
            if (!ModelState.IsValid)
                return false;

            if (ModelState.IsValid) {
                var request = new UpdateDepartmentRequest(formModel.Id);
                request.DepartmentProperties = formModel;
                var response = _settingAppService.UpdateDepartment(request);

                if (response.Exception == null)
                    return true;
            }

            return false;
        }

        private DepartmentFormModel ConstructForm(DepartmentViewModel vmodel) {
            return new DepartmentFormModel {
                Contact = vmodel.Contact,
                Mobile = vmodel.Mobile,
                Id = vmodel.Id,
                DepartmentName = vmodel.DepartmentName,
                Email = vmodel.Email,
                Fax = vmodel.Fax,
                Tel = vmodel.Tel
            };
        }
    }
}
